﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Daneas.Utility.Universals
{
    public static class StringPlus
    {
        public static List<string> GetStrArray(string str, char speater, bool toLower)
        {
            List<string> list = new List<string>();
            string[] ss = str.Split(speater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) && s != speater.ToString())
                {
                    string strVal = s;
                    if (toLower)
                    {
                        strVal = s.ToLower();
                    }
                    list.Add(strVal);
                }
            }
            return list;
        }

        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[',']);
        }

        public static string GetArrayStr(List<string> list, string speater)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    sb.Append(list[i]);
                }
                else
                {
                    sb.Append(list[i]);
                    sb.Append(speater);
                }
            }
            return sb.ToString();
        }

        public static List<string> GetSubStringList(string o_str, char sepeater)
        {
            List<string> list = new List<string>();
            string[] ss = o_str.Split(sepeater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
                {
                    list.Add(s);
                }
            }
            return list;
        }

        public static string ToStr(this byte[] bs, Encoding encode)
        {
            return encode.GetString(bs);
        }

        public static byte[] ToByte(this string str, Encoding encode)
        {
            return encode.GetBytes(str);
        }


        /// <summary>
        /// 获取长度文本
        /// </summary>
        /// <param name="text"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetStr(this string text, int length)
        {
            if (string.IsNullOrEmpty(text))
            {
                return "";
            }

            if (text.Length < length)
            {
                return text;
            }

            return text.Substring(0, length);
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public static int ConvertDateTimeInt(this DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        public static DateTime GetTime(this string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        #region 格式转换
        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSBC(string input)
        {
            //半角转全角：
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }

        /// <summary>
        ///  转半角的函数(SBC case)
        /// </summary>
        /// <param name="input">输入</param>
        /// <returns></returns>
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        /// <summary>
        /// 将string转换成int
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt32(this string str)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(str))
                {
                    return Int32.MinValue;
                }

                return Convert.ToInt32(str);
            }
            catch
            {
                return int.MinValue;
            }
        }

        /// <summary>
        /// 将string转换成long
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static long ToInt64(this string str)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(str))
                {
                    return Int64.MinValue;
                }

                return Convert.ToInt64(str);
            }
            catch
            {
                return long.MinValue;
            }
        }

        public static double ToDouble(this string str)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(str))
                {
                    return Double.MinValue;
                }

                return Convert.ToDouble(str);
            }
            catch
            {
                return double.MinValue;
            }
        }

        public static DateTime ToDateTime(this string str)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(str))
                {
                    return DateTime.MinValue;
                }

                return Convert.ToDateTime(str);
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// 返回格式为1391999390的时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimestamp()
        {
            DateTime aa = new DateTime(1970, 1, 1);
            return ((DateTime.Now.Ticks - aa.Ticks) / 10000000 - 8 * 60 * 60);
        }
        #endregion

        #region string解码编码

        /// <summary>
        /// 转为Unicode编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToUnicode(this string str)
        {
            byte[] b = Encoding.Unicode.GetBytes(str);
            string o = "";
            foreach (var x in b)
            {
                o += string.Format("{0:X2}", x) + " ";
            }
            return o;
        }

        /// <summary>
        /// Unicode解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DeUnicode(this string str)
        {
            string cd2 = str.Replace(" ", "");
            cd2 = cd2.Replace("\r", "");
            cd2 = cd2.Replace("\n", "");
            cd2 = cd2.Replace("\r\n", "");
            cd2 = cd2.Replace("\t", "");
            if (cd2.Length % 4 != 0)
            {
                return "";
            }

            int len = cd2.Length / 2;
            byte[] b = new byte[len];
            for (int i = 0; i < cd2.Length; i += 2)
            {
                string bi = cd2.Substring(i, 2);
                b[i / 2] = (byte)Convert.ToInt32(bi, 16);
            }
            string o = Encoding.Unicode.GetString(b);
            return o;

        }

        /// <summary>
        /// 转换成UTF8
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToUTF8(this string str)
        {
            byte[] bts = Encoding.UTF8.GetBytes(str);
            string o = "";
            foreach (byte b in bts)
            {
                o += b.ToString();
            }

            return o;
        }

        /// <summary>
        /// UTF8解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DeUTF8(this string str)
        {
            byte[] bs = UTF8Encoding.UTF8.GetBytes(str);

            string o = "";
            foreach (byte b in bs)
            {
                o += b.ToString();
            }

            return o;
        }

        #endregion

        #region Base64的加密解密

        /// <summary>
        /// Base64加密
        /// </summary>
        /// <param name="codeName">加密采用的编码方式</param>
        /// <param name="source">待加密的明文</param>
        /// <returns></returns>
        public static string EncodeBase64(Encoding encode, string source)
        {
            byte[] bytes = encode.GetBytes(source);
            try
            {
                return Convert.ToBase64String(bytes);
            }
            catch
            {
                return source;
            }
        }

        /// <summary>
        /// Base64加密，采用utf8编码方式加密
        /// </summary>
        /// <param name="source">待加密的明文</param>
        /// <returns>加密后的字符串</returns>
        public static string EncodeBase64(string source)
        {
            return EncodeBase64(Encoding.UTF8, source);
        }

        /// <summary>
        /// Base64解密
        /// </summary>
        /// <param name="codeName">解密采用的编码方式，注意和加密时采用的方式一致</param>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string DecodeBase64(Encoding encode, string result)
        {
            byte[] bytes = Convert.FromBase64String(result);
            try
            {
                return encode.GetString(bytes);
            }
            catch
            {
                return result;
            }
        }

        /// <summary>
        /// Base64解密，采用utf8编码方式解密
        /// </summary>
        /// <param name="result">待解密的密文</param>
        /// <returns>解密后的字符串</returns>
        public static string DecodeBase64(string result)
        {
            return DecodeBase64(Encoding.UTF8, result);
        }
        #endregion
    }
}
